import {
  myGetRequest,
  myPostRequest
} from "../../utils/myRequest.js"
//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    mypose:"",
    routeId:'0',
    photoState:'0'
  },
    onLoad: function (options){
        var that = this;
        console.log("页面初始化");
        var userInfo;
        try {
           userInfo = wx.getStorageSync('userInfo');
          if (!userInfo) {
            wx.redirectTo({
              url: '../login/login'
            })
          }
        } catch (e) {
          // Do something when catch error
          console.log("页面初始化失败： "+e)
        }
        that.setData({
          userInfo : userInfo
        })
        let temrouteid = JSON.parse(options.routeId)
        console.log(temrouteid+"erwrewrewr");
        that.setData({
         userInfo:wx.getStorageSync('userInfo'),
         routeId:temrouteid
       })
        wx.request({
          url: 'https://jianbuxing.qdszgh.cn/complete/findphotostate', // 服务器后端的地址
          data: {
            "id": userInfo.id,
            "routeId": this.data.routeId
            // 传给后端的数据
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
          },
  
          method: "POST", // 请求的方式有Get和Post两种
          success(res) {
            that.setData({
              photoState:res.data
            })
            console.log(that.data.photoState)
            console.log(res.data)
            // 这里写请求成功后会执行的代码
          }
        })
        that.setData({
         userInfo:wx.getStorageSync('userInfo'),
          routeId:options.routeId
       })

      },

    choice: function () {
      var that = this
      wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths
          that.setData({
            textHidden: true,
            image_photo: tempFilePaths,
            photoHidden: false
          })
          that.uploadPhoto();          
        }
      })

    },
    uploadPhoto: function () {
      var that = this
      console.log('临时路径：' + that.data.image_photo[0])
      console.log(this.data.mypose)
      var userInfo = wx.getStorageSync('userInfo')
      console.log(userInfo.id)
      console.log(that.data.routeId)
      wx.uploadFile({
        url: 'https://jianbuxing.qdszgh.cn/photo/uploadFile2', //仅为示例
        formData: {
          mypose: this.data.mypose,
          userid:userInfo.id,
          routeid:this.data.routeId,
          // 传给后端的数据
        },
        filePath: that.data.image_photo[0],
        name: 'myfile',
        header: {
          'content-type': 'multipart/form-data'
        },
        success: function (res) {
          if (res.statusCode == 200) {
            wx.showToast({
              title: '上传成功',
              icon: 'suceess',
            })
          } else {
            wx.showToast({
              title: '上传失败',
              icon: 'error',
            })
          }
        }
      })

    },
    signForPower: function () {
      var that = this
      var userInfo = wx.getStorageSync('userInfo')
      //  console.log(JSON.stringify(userInfo))

      wx.request({
        url: 'https://jianbuxing.qdszgh.cn/user/signforpower', // 服务器后端的地址
        data: {
          "id": userInfo.id,
          // 传给后端的数据
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
        },

        method: "POST", // 请求的方式有Get和Post两种
        success(res) {
          console.log(userInfo.id)
          if (res.statusCode == 200) {
            console.log(res.data.message)
            if (res.data.message == "sameday") {
              wx.showToast({
                title: '已签到过',
                icon: 'error',
              })
            } else {
              wx.showToast({
                title: '兑换成功',
                icon: 'suceess',
              })
            }

          } else {
            wx.showToast({
              title: '兑换失败',
              icon: 'error',
            })
          }
          // 这里写请求成功后会执行的代码
        }
      })

    },
    makepose: function () {
      var that = this
      var userInfo = wx.getStorageSync('userInfo')
      //  console.log(JSON.stringify(userInfo))
      wx.getLocation({
        type: 'wgs84',
        success (res) {
          wx.request({
            url: 'https://jianbuxing.qdszgh.cn/pose/randompose', // 服务器后端的地址
            data: {
              // 传给后端的数据
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
            },
    
            method: "POST", // 请求的方式有Get和Post两种
            success(res) {
                  that.setData({mypose:res.data.data})

              // 这里写请求成功后会执行的代码
            }
          })


        }
       })


    },
})

